/*
 * ListBoxFiller.java
 *
 * Created on 6. September 2007, 10:04
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.googlecode.oskis.library.db;
import java.sql.*;
import javax.swing.JList;
import javax.swing.DefaultListModel;

/**
 *
 * @author thomas
 */
public class ListBoxFiller {
    
    private Connection db;
    private String sqlerror;
    
    /** Creates a new instance of ListBoxFiller */
    public ListBoxFiller(Connection db) {
        this.db=db;
    }
    
    public boolean fill(JList l,String SQL) {
        Statement st;
        ResultSet rs;
        DefaultListModel dlm=new DefaultListModel();
        try {
            st=db.createStatement();
            rs=st.executeQuery(SQL);
            while(rs.next()) {
                ComboBoxEintrag ce=new ComboBoxEintrag(rs.getString(1),rs.getString(2));
                dlm.addElement(ce);
            }
            rs.close();
            st.close();
        } catch (Exception e) {
            sqlerror=e.toString();
            return(false);
        }        
        l.setModel(dlm);
        return(true);  
    }
    
    public String getSQLError() {
        return(sqlerror);
    }
    
}
